package com.hy.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.hy.bean.Messages;
import com.hy.bean.PageBean;
import com.hy.bean.UmsUser;
import com.hy.dao.MessagesDao;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.List;
import java.util.Map;

public class MessagesController {
    private MessagesDao messagesDao = new MessagesDao();
    private ObjectMapper objectMapper =new ObjectMapper();


    public boolean selectId(HttpServletRequest request, HttpServletResponse response) throws Exception {
        HttpSession session = request.getSession();
        int id = (int)session.getAttribute("id");

        PrintWriter out = response.getWriter();
        String user_name = request.getParameter("user_name");
        String content = request.getParameter("message");

        UmsUser umsUser = messagesDao.selectId(user_name);

        System.out.println(umsUser.toString());

        PageBean<List<Map<String, Object>>> pb = new PageBean<>();
         Messages messages = new Messages(id,umsUser.getId(),new Date(),id,null,content);

        boolean flag = messagesDao.insert(messages);
        System.out.println(flag);
        return  flag;
    }

    public  PageBean<List<Map<String, Object>>> select(HttpServletRequest request,HttpServletResponse response) throws IOException {
        HttpSession session = request.getSession();
        int id = (int)session.getAttribute("id");

        PrintWriter out = response.getWriter();
        PageBean<List<Map<String, Object>>> pb = new PageBean<>();
        try {

            List<Map<String, Object>> maps = messagesDao.select(id);
            System.out.println(maps.toString());
            if (maps != null){
                pb.setMsg("数据传入成功");
                pb.setStatus(200);
                pb.setData(maps);
                pb.setCode(000100);
                //沙沟
//                String s = objectMapper.writeValueAsString(pb);
//
//                out.write(s);
//                out.flush();
//                out.close();
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return  pb;
    }
    public PageBean<List<Map<String, Object>>> selectavatar(HttpServletRequest request, HttpServletResponse response) throws IOException {
        HttpSession session = request.getSession();
        int id = (int)session.getAttribute("id");

        PrintWriter out = response.getWriter();

        System.out.println(id);

        PageBean<List<Map<String, Object>>> pb = new PageBean<>();

        try {

            List<Map<String, Object>> maps = messagesDao.select(id);
            System.out.println(maps.toString());
            if (maps != null){
                pb.setMsg("数据传入成功");
                pb.setStatus(200);
                pb.setData(maps);
                pb.setCode(000100);
                //沙沟
//                String s = objectMapper.writeValueAsString(pb);
//
//                out.write(s);
//                out.flush();
//                out.close();
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return  pb;
    }


    public PageBean<List<Map<String, Object>>> select1(HttpServletRequest request, HttpServletResponse response){
        HttpSession session = request.getSession();
        String username = (String) session.getAttribute("username");

        PageBean<List<Map<String, Object>>> pb = new PageBean<>();

        try {
            List<Map<String, Object>> maps = messagesDao.select1(username);

            if (maps != null) {
                pb.setMsg("数据传入成功");
                pb.setStatus(200);
                pb.setData(maps);
                pb.setCode(000100);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

        return  pb;
    }

    public PageBean<List<Map<String, Object>>> select2(HttpServletRequest request, HttpServletResponse response){
        String sid = request.getParameter("id");
        int id = 0;
       if (sid != null){
            id = Integer.parseInt(sid);
       }


        PageBean<List<Map<String, Object>>> pb = new PageBean<>();

        try {
            List<Map<String, Object>> maps = messagesDao.select2(id);

            if (maps != null) {
                pb.setMsg("数据传入成功");
                pb.setStatus(200);
                pb.setData(maps);
                pb.setCode(000100);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return  pb;
    }

    public PageBean<List<Map<String, Object>>> select3(HttpServletRequest request, HttpServletResponse response){
        String user_name = request.getParameter("user_name");
        System.out.println(user_name);
        PageBean<List<Map<String, Object>>> pb = new PageBean<>();
        try {
            List<Map<String, Object>> maps = messagesDao.select3(user_name);

            if (maps != null) {
                pb.setMsg("数据传入成功");
                pb.setStatus(200);
                pb.setData(maps);
                pb.setCode(000100);
            }else {
                pb.setMsg("没有这人");
                pb.setStatus(500);
                pb.setData(null);
                pb.setCode(000100);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return  pb;
    }


}
